package com.cincc.cincc_pbx;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.multidex.MultiDex;

import com.cincc.cincc_pbx.common.CallUtil;
import com.cincc.cincc_pbx.constant.CallConstant;
import com.cincc.cincc_pbx.model.EventBusModel;
import com.cincc.cincc_pbx.ui.ExternalActivity;
import com.cincc.cincc_pbx.ui.InternalActivity;
import com.cincc.cincc_pbx.ui.MainActivity;
import com.tencent.bugly.crashreport.CrashReport;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * @company Name:
 * @author: sky
 * @creatDate: 2022/3/15 4:33 下午
 * @className: GlobalApp
 * @description:
 * @modified By:
 * @modifyDate:
 */
public class GlobalApp extends Application {

    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();
        MultiDex.install(this);
        CrashReport.initCrashReport(getApplicationContext(), "59a6e5af8f", true);
        register();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new MyLifecycleObserver());
    }

    static Activity currentActivity;

    private void register() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                Log.d("fengzi", "[AppApplication.java] -- onActivityResumed -- activity:" + activity);
                currentActivity = activity;
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                Log.d("fengzi", "[AppApplication.java] -- onActivityStopped -- activity:" + activity);
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                Log.d("fengzi", "[AppApplication.java] -- onActivityDestroyed -- activity:" + activity);
            }
        });
    }

    class MyLifecycleObserver implements LifecycleObserver {
        // 方法名随便取，注解才是重点
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        void onForeground() {
            Log.e("fengzi", "应用回到前台");
            if (GlobalApp.currentActivity instanceof MainActivity || GlobalApp.currentActivity instanceof ExternalActivity || GlobalApp.currentActivity instanceof InternalActivity)
                EventBus.getDefault().post(new EventBusModel(CallConstant.RE_REGISTER, null));
            Log.e("fengzi", "onForeground: currentActivity" + GlobalApp.currentActivity);
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        void onBackground() {
            Log.e("fengzi", "应用退到后台");
            if (GlobalApp.currentActivity instanceof MainActivity || GlobalApp.currentActivity instanceof ExternalActivity || GlobalApp.currentActivity instanceof InternalActivity)
                CallUtil.getInstance().UnInitial();
        }
    }
}
